package com.kdtech.analyse.blog;
import com.kdtech.crawler.CrawlHTML;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;


import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.NumberUtils;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;

/**
 * 百度空间解析
 * @author KK
 *
 */
public class HiBaiduBlogAnalyse implements AnalyseNews{

	
	public boolean isDetailPage(String url) {
		String[] regex = {
				"http://hi.baidu.com/[a-z0-9]*/item/[0-z0-9]{24}",
				};
		return RegexUtils.matchAny(url, regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {

		if (urlMeta.getHtml() == null) {
		}
		String htmltxt = urlMeta.getHtml();
		String url = urlMeta.getUrl();

		String title = null;
		String content = null;
		Long date = null;
		String dateStr = null;
		String commnetNum = null;
		String clickNum = null;
		String deliverNum = null;
		String updateUrl = url;
		String author = null;
		Document doc = Jsoup.parse(htmltxt);

		author = doc.select("h1 a.space-name").text();
		if(author.endsWith("的空间")){
			author = author.substring(0,author.length()-3);
		}
		title = doc.select("title").text();
		if (StringUtils.isNotEmpty(title)){
			title = StringUtils.substringBeforeLast(title, "_");
			title = StringUtils.substringBeforeLast(title, "_");
		}

		dateStr = doc.select("div.content-head").text();
		content = HtmlCleaner.getContentHtml(url,  doc.select("div#content"));

		String text = doc.select("div.mod-post-info div.op-box").text();
		if (StringUtils.isNotBlank(text)){
			clickNum =StringUtils.substringBetween(text, "浏览(",")");
			commnetNum =StringUtils.substringBetween(text, "评论(",")");
			deliverNum =StringUtils.substringBetween(text, "转载(",")");
		}
		date = DateUtils.matchDate(dateStr);
		if (date==null){
			date = DateUtils.matchDate(dateStr);
		}

			NewsMeta news = new NewsMeta();
			news.setUrl(url);
			news.setAuthor(author);
			news.setType(7);
			news.setTitle(StringUtils.trimSpace(title));
			news.setCommentNum(NumberUtils.parseInt(commnetNum));
			news.setClickNum(NumberUtils.parseInt(clickNum));
//			news.setDeliverNum(NumberUtils.parseInt(deliverNum));
			news.setContent(StringUtils.trimSpace(content));
			news.setDate(date);
			news.setUpdateUrl(updateUrl);

			return news;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		if (meta!=null &&StringUtils.isNotBlank(meta.getUpdateUrl())){
			UrlMeta urlMeta = CrawlHTML.responseToURL(meta.getUpdateUrl());
			if (urlMeta!=null && urlMeta.getHtml()!=null){
				return parserHtml(urlMeta);
			}
		}
		return null;
	}





	public static void main(String[] args) {
		String url = "http://hi.baidu.com/dadbabymom/item/e01d5a00e35d23f5a1103467";
		HiBaiduBlogAnalyse test = new HiBaiduBlogAnalyse();
		if(test.isDetailPage(url)){
			UrlMeta responseToURL = CrawlHTML.responseToURL(url);
			NewsMeta parserHtml = test.parserHtml(responseToURL);
			System.out.println(parserHtml);
		}else{
			System.out.println("不符合规则");
		}
	}

	
}
